// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Manages tabs behavior for 'theme_rlmaster' admin settings
 *
 * @package   theme-rlmaster
 * @author    Amy Groshek <amy@remote-learner.net>
 * @copyright 2013 Remote Learner  http://www.remote-learner.net/
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

/*
 * This function submits the admin form
 * to display.
 * @param integer id Id of the tab
 * @param integer tab Value of the tab
 * @return boolean false
 */
function rlmaster_change_field(id, tab) {
    var tab_field = document.getElementById(id);
    var admin_form = document.getElementById('adminsettings');
    var old = tab_field.value;

    tab_field.value = tab;
    admin_form.submit();
    tab_field.value = old;

    return false;
}

/**
 * Manage missing objects if necessary
 */
if (!M) {
    M = {};
}

if (!M.theme) {
    M.theme = {};
}

if (!M.theme.rlmaster) {
    M.theme.rlmaster = {};
}

M.theme.rlmaster.manage_tabs = {
    /**
     * Attach listener to all unselected tabs. When
     * clicked, new tab index is saved to hidden input
     * and form is submitted.
     *
     * @param Object Y YUI3 JavaScript library
     */
    listen_tabs : function(Y) {
        var $alltabs = Y.all('#adminsettings .tabtree ul li a'); // 'a.rlmaster-admin-tab');
        var $targettabs = $alltabs.filter('.rlmaster-admin-tab');
        var $submittarget = Y.one('#adminsettings');

        var tabaction = $targettabs.on('click', function(e) {
            // Stop default behavior and bubbling
            e.preventDefault();
            e.stopPropagation();

            // Get index of the clicked tab
            var $index = $alltabs.indexOf(e.currentTarget);
            // Get size of tabs collection
            var $size = $alltabs.size();
            // Get hidden input which will receive tab index
            var $tab_field = Y.one('#id_s_theme_rlmaster_tabs');

            // If tab is the last in the series,
            // set hidden input value to 99.
            // Else set hidden input to tab index.
            if (($index + 1) === $size) {
                $tab_field.set('value', 99);
            } else {
                $tab_field.set('value', $index);
            }

            // Prevent annoying prompt that a form field was changed
            window.onbeforeunload = null;

            // Submit form
            $submittarget.submit();
        });
    },
    /**
     * Init add listener to unselected tabs.
     *
     * @param Object Y YUI3 JavaScript library
     */
    init : function(Y) {
        this.listen_tabs(Y);
    }
};
